﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using GameSchoolApplication.Models;
using System;
using GameSchoolApplication.Interfaces;

namespace GameSchoolApplication.Repository
{
	public class PersonRepository : IPersonRepository
	{
		private  GameSchoolWebContext _db = new GameSchoolWebContext();


		/// <summary>
		/// Returns the list of all persons
		/// </summary>
		/// <returns></returns>
		public IEnumerable<Person> GetAllPersons()
		{
			return _db.Persons.ToList();
		}

		/// <summary>
		/// Returns a person by id:Int
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Person GetPersonById(int? id)
		{
			var person = _db.Persons.Where(m => m.Id == id).SingleOrDefault();
			return person;
		}

		/// <summary>
		/// Creates a person
		/// </summary>
		/// <param name="person"></param>
		public void CreatePerson(Person person)
		{
			_db.Persons.Add(person);
            _db.SaveChanges();
		}		

		/// <summary>
		/// Removes a person
		/// </summary>
		/// <param name="personToDelete"></param>
		public void DeletePerson(Person personToDelete)
		{
			_db.Persons.Remove(personToDelete);
			_db.SaveChanges();
		}
		
		/// <summary>
		/// Updates a person
		/// </summary>
		/// <param name="person"></param>
		public void UpdatePerson(Person person)
		{
			_db.Entry(person).State = EntityState.Modified;
			_db.SaveChanges();
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing){}
	}
}